// pages/myStudent/myStudent.js
var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        loading: true,
        navIndex: 0,
        isFocus: false,
        list: [],
        page: 1,
        total: 0
    },
    navItem: function(e) {
        this.setData({
            page: 1
        })
        var status = e.currentTarget.dataset.id
        // this.setData({
        //     navIndex: status
        // })
        if (status == 0) {
          this.getList('c=PreOrder&m=lists', 1, false, status)
        } else if (status == 1) {
          this.getList('c=order&m=lists', 1, false, status)
        } else if (status == 2) {
          this.getList('c=order&m=paymentList', 1, false, status)
        }
    },
    searchFocus: function() {
        this.setData({
            isFocus: true
        })
    },
    searchBlur: function(e) {
        if (!e.detail.value) {
            this.setData({
                isFocus: false
            })
        }
    },
    search: function(e) {
        var keyword = e.detail.value
        if (this.data.navIndex == 0) {
            this.getList('c=PreOrder&m=lists&keyword=' + keyword, 1, false)
        } else if (this.data.navIndex == 1) {
            this.getList('c=order&m=lists&keyword=' + keyword, 1, false)
        } else if (this.data.navIndex == 2) {
            this.getList('c=order&m=paymentList&keyword=' + keyword, 1, false)
        }
    },
    detail: function(e) {
        var is_prepare = 0
        if (this.data.navIndex == 0) {
            is_prepare = 1
        }
        wx.navigateTo({
            url: '/pages/guidanceStudy/studentDetail/studentDetail?order_id=' + e.currentTarget.dataset.id + '&is_prepare=' + is_prepare
        })
    },
    /**
     * url: 接口地址
     * page: 页码
     * isPaging: 是否分页
     */
    getList: function(url, page, isPaging, status) {
        var status = status || '';
        var that = this;
        wx.showLoading({
            title: '加载中...'
        })
        wx.request({
            url: app.globalData.url + url + '&user_id=' + wx.getStorageSync('user_id') + '&page=' + page + '&pageSize=20',
            success: res => {
                wx.hideLoading()
                res = res.data
                if (res.status === 200) {
                    if (isPaging) {
                        that.setData({
                            list: that.data.list.concat(res.data.orders),
                            total: res.data.paging.total
                        })
                    } else {
                        if(status) {
                          this.setData({
                            navIndex: status
                          });
                        }
                        that.setData({
                            list: res.data.orders,
                            total: res.data.paging.total
                        });
                    }
                } else {
                    wx.showToast({
                        title: res.msg,
                        icon: 'none'
                    });
                }
            }
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getList('c=PreOrder&m=lists', this.data.page, false);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.list.length < this.data.total) {
            var page = this.data.page
            page++
            this.setData({
                page: page
            })

            if (this.data.navIndex == 0) {
                this.getList('c=PreOrder&m=lists', page, true)
            } else if (this.data.navIndex == 1) {
                this.getList('c=order&m=lists', page, true)
            } else if (this.data.navIndex == 2) {
                this.getList('c=order&m=paymentList', page, true)
            }
        } else {
            wx.showToast({
                title: '没有更多数据',
                icon: "none"
            })
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})